package cn.codeforfun.jfinalplugin.activemq.core;

import com.jfinal.log.Log;

import javax.jms.JMSException;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import java.util.HashMap;
import java.util.Map;

public abstract class JFinalTopic extends JFinalActiveMq {
    private static Log log = Log.getLog(JFinalTopic.class);

    private Map<String, MessageListener> listenerHashMap = new HashMap<>();
//    private String user = ActiveMQConnection.DEFAULT_USER;
//    private String password = ActiveMQConnection.DEFAULT_PASSWORD;
//    private String brokerUrl = ActiveMQConnection.DEFAULT_BROKER_URL;
    private String topicName = null;
//    private ConnectionFactory connectionFactory;
//    private Connection connection = null;
//    private Session session;
//    private Destination destination;
//    private MessageProducer producer;
//    private MessageConsumer consumer;
//    private boolean isStarted = false;

//    public JFinalTopic(String topicName) {
//        this.topicName = topicName;
//        init();
//        super(topicName);
//    }

    static Map<String, JFinalTopic> topicMap = new HashMap<>();

//    public JFinalTopic setUser(String user) {
//        this.user = user;
//        return this;
//    }
//
//    public JFinalTopic setPassword(String password) {
//        this.password = password;
//        return this;
//    }
//
//    public JFinalTopic setBrokerUrl(String brokerUrl) {
//        this.brokerUrl = brokerUrl;
//        return this;
//    }

    public JFinalTopic setTopicName(String topicName) {
        this.topicName = topicName;
        return this;
    }

    protected String getTopicName() {
        return topicName;
    }
//    public MessageConsumer getConsumer(){ return consumer().consumer; }

//    public static boolean check_message(String message){
//        if (StrKit.isBlank(message)) {
//            log.debug("Message is blank.");
//            return false;
//        }
//        return true;
//    }

//    public static boolean check_topicName(String topicName){
//        if (!ActiveMqPlugin.topicMap.containsKey(topicName)) {
//            log.error("The topic is not exist.The topic name is " + topicName);
//            return false;
//        }
//        return true;
//    }

//    public JFinalTopic connection(){
//        connectionFactory = new ActiveMQConnectionFactory(
//                ActiveMQConnection.DEFAULT_USER,
//                ActiveMQConnection.DEFAULT_PASSWORD,
//                ActiveMQConnection.DEFAULT_BROKER_URL);
//        try { // 构造从工厂得到连接对象
//            connection = connectionFactory.createConnection();
//            connection.start(); // 启动
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
//        return this;
//    }

//    public JFinalTopic destination(){
//        try { // 获取操作连接
//            session = connection.createSession(Boolean.FALSE,
//                    Session.AUTO_ACKNOWLEDGE);
//            destination = session.createTopic(topicName);
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
//        return this;
//    }

//    public JFinalTopic init(){
//        if (topicName == null) {
//            log.error("Topic name can't be null.");
//            throw new NullPointerException();
//        } else {
//            connection();
//            destination();
//        }
//        return this;
//    }

//    public JFinalTopic consumer(Destination destination){
//        try {
//            if(consumer == null){
//                consumer = session.createConsumer(destination);
//            }
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
//        return this;
//    }
//
//    public JFinalTopic consumer(){
//        return consumer(destination);
//    }

    //cmd=(start,stop)
    static void optTopic(String cmd){
        JFinalTopic.topicMap.forEach((k,v)->{
            switch (cmd) {
                case "start":
                    try {
                        if(v.isStarted() || v.startMq()){
                            log.debug("The topic has been started.The name is " + v.getTopicName());
                        }
                        log.info("JFinal topic has been started.");
                    } catch (Exception e) {
                        log.info("JFinal topic start error.");
                        e.printStackTrace();
                    }
                    break;

                case "stop":
                    try {
                        if(v.isStarted() && v.stopMq()){
                            log.debug("The queue has been stopped.The name is " + v.getTopicName());
                        }
                        log.info("JFinal queue has been stopped.");
                    } catch (Exception e) {
                        log.info("JFinal queue stop error.");
                        e.printStackTrace();
                    }
                    break;
            }
        });
    }

    protected JFinalTopic addListener(MessageListener messageListener) {
        if (listenerHashMap.containsKey(messageListener.getClass().getName())) {
            log.error("The topic listener is exist.Class name is " + messageListener.getClass().getName());
        }
        listenerHashMap.put(messageListener.getClass().getName(), messageListener);
        return this;
    }

    protected JFinalTopic listenerHashMap() {
        listenerHashMap.forEach((k,v)->{
            try {
                getConsumer().setMessageListener(v);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        });
        return this;
    }

//    public abstract void run();

//    public boolean startTopic() {
//        if (isStarted) return true;
//        Thread thread = new Thread(this);
//        thread.start();
//        isStarted = true;
//        return true;
//    }
//
//    public boolean stopTopic() {
//        if (!isStarted) return true;
//        try {
//            this.connection.close();
//        } catch (JMSException e) {
//            e.printStackTrace();
//        }
//        isStarted = false;
//        return true;
//    }

    static boolean check_mqName(String mqName){
        if (!topicMap.containsKey(mqName)) {
            log.error("The mq is not exist.The mq name is " + mqName);
            return false;
        }
        return true;
    }

    public static void sendMessage(String topicName, String message) {
        if(check_message(message) && check_mqName(topicName)){
            JFinalTopic topic = topicMap.get(topicName);
            try {
                TextMessage textMessage = topic.getSession().createTextMessage();
                textMessage.setText(message);
                MessageProducer producer = topic.getSession().createProducer(topic.getDestination());
                producer.send(textMessage);
            } catch (JMSException e) {
                log.error("Send message error.");
                e.printStackTrace();
            }
        }
    }

}
